package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.vo.ActivityListVo;
import com.ruoyi.domain.vo.ActivityPunchingVo;
import com.ruoyi.service.ILowCarbonActivityService;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.ActivityApply;
import com.ruoyi.system.domain.ActivityLogs;
import com.ruoyi.system.domain.UserInfo;
import com.ruoyi.system.service.IActivityLogsService;
import com.ruoyi.system.service.IActivityService;
import com.ruoyi.system.service.impl.UserInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/wechatApp/lowCarbonActivity")
public class LowCarbonActivityController extends BaseController {

    @Autowired
    private ILowCarbonActivityService lowCarbonActivityService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityLogsService activityLogsService;
    @Autowired
    private UserInfoServiceImpl userInfoServiceImpl;

    /*
     * 获取活动信息列表调用若依接口
     */

//    @GetMapping("/getActivityList")
//    public AjaxResult getActivityList(@RequestParam("activity")Activity activity, @RequestParam("pageDTO") PageDTO pageDTO) {
//        return AjaxResult.success(lowCarbonActivityService.getActivityList(activity, pageDTO));
//    }

    /**
     * 报名活动
     *
     * @param activityId 活动id
     * @param userId     用户id
     * @return 报名情况
     */
    @PostMapping("/applicationActivity")
    @ResponseBody
    public AjaxResult applicationActivity(@RequestParam("activityId") Long activityId, @RequestParam("userId") Long userId) {
        Activity activity = activityService.selectActivityByActivityId(activityId);
        ActivityLogs activityLog = activityLogsService.selectActivityLogsByActivityIdAndUserId(activityId, userId);
        UserInfo userInfo = userInfoServiceImpl.selectUserInfoByUserId(userId);
        System.out.println(activityLog);
        if (activityLog != null) {
            return AjaxResult.error("您已报名，请勿重复报名");
        } else if (activity == null || activity.getApplyEndTime() == null || activity.getApplyEndTime().compareTo(new Date()) < 0) {
            return AjaxResult.error("活动报名时间已结束或不存在");
        } else {
            ActivityLogs newActivityLog = new ActivityLogs();
            newActivityLog.setActivityId(activityId);
            newActivityLog.setActivityName(activity.getActivityName());
            newActivityLog.setUserId(userId);
            newActivityLog.setnickName(userInfo.getNickName());
            newActivityLog.setApplyTime(new Date());
            newActivityLog.setCreateTime(new Date());
            newActivityLog.setUpdateTime(new Date());
            activityLogsService.insertActivityLogs(newActivityLog);
            activity.setApplyMembers(activity.getApplyMembers() + 1);
            activityService.updateActivity(activity);
            return AjaxResult.success("报名成功");
        }
    }

    /**
     * 取消报名
     *
     * @param activityId    活动id
     * @return 取消情况
     */
    @PostMapping("/cancelApplicationActivity")
    @ResponseBody
    public AjaxResult cancelApplicationActivity(@RequestParam("activityId") Long activityId, @RequestParam("userId") Long userId) {
        ActivityLogs activityLog = activityLogsService.selectActivityLogsByActivityIdAndUserId(activityId, userId);
        if (activityLog == null) {
            return AjaxResult.error("取消报名失败，活动不存在或您未报名");
        }
        activityLogsService.deleteActivityLogsByActivityIdAndUserId(activityId, userId);
        Activity activity = activityService.selectActivityByActivityId(activityId);
        activity.setApplyMembers(activity.getApplyMembers() - 1);
        activityService.updateActivity(activity);
        return AjaxResult.success("取消报名成功");
    }

    /**
     * 申请组织活动
     *
     * @param activityApply 申请活动信息
     * @return 申请结果
     */
    @PostMapping("/organizeActivity")
    @ResponseBody
    public AjaxResult organizeActivity(@RequestBody ActivityApply activityApply) {
        lowCarbonActivityService.organizeActivity(activityApply);
        return AjaxResult.success();
    }

    /**
     * 获取活动列表,如果用户报名了活动，则显示已报名状态，否则显示报名状态；
     * 根据活动不可见，仅组织可见，所有人可见三种可见状态筛选活动列表
     *
     * @param userId,pageNum,pageSize 用户id 页码 每页条数
     * @return 活动列表
     */
    @GetMapping("/getActivityList")
    public AjaxResult getActivityList(@RequestParam("userId") Long userId, @RequestParam("pageNum") Long pageNum, @RequestParam("pageSize") Long pageSize, @RequestParam(value = "activityName", required = false) String activityName) {
        List<ActivityListVo> activityList = lowCarbonActivityService.getActivityList(userId, pageNum, pageSize, activityName);
        return AjaxResult.success(activityList);
    }

    @GetMapping("/getActivityListByOrganization")
    @ResponseBody
    public AjaxResult getActivityListByOrganization(@RequestParam("organization") String organization, @RequestParam("pageNum") Long pageNum, @RequestParam("pageSize") Long pageSize) {
        startPage();
        List<Activity> activityList = lowCarbonActivityService.getActivityListByOrganization(organization, pageNum, pageSize);
        return AjaxResult.success(activityList);
    }

    /**
     * 低碳活动打卡
     * @param activityPunchingVo
     * @return 打卡结果
     */
    @PostMapping("/ActivityPunching")
    public AjaxResult ActivityPunching(@RequestBody ActivityPunchingVo activityPunchingVo) {
        lowCarbonActivityService.ActivityPunching(activityPunchingVo);
        return AjaxResult.success();
    }

    /**
     * 同意申请活动
     * @param activityApplyId 申请活动id
     * @return 同意结果
     */
    @PostMapping("/agreeApply")
    public AjaxResult agreeApply(@RequestParam("activityApplyId") Long activityApplyId) {
        return AjaxResult.success(lowCarbonActivityService.agreeApply(activityApplyId));
    }

    /**
     * 获取低碳活动打卡历史信息
     * @param participateId 活动参与Id
     * @return 打卡历史信息
     */
    @GetMapping("/getActivityLogs")
    public AjaxResult getActivityLogs(@RequestParam("participateId") Long participateId) {
        return AjaxResult.success(lowCarbonActivityService.getActivityLogs(participateId));
    }
}
